*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;   
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;


*Figures from Latinobarometer

*Only enough data for Mercosur; Andean Pact too few data points.
*IMF and World Bank would be available

use dataset_Latinobarometer_Afrobarometer_2000-2015-wide.dta, clear
by year, sort:tab country
sort country year
ed country year mercosur_trust if mercosur_trust!=.
tab country year  if mercosur_trust!=.
*32 Argentina<<<
*68 Bolivia
*76 Brazil<<<
*152 Chile<<<
*188 Costa Rica
*218 Ecuador
*484 Mexico<<<
*558 Nicaragua
*604 Peru<<<
*862 Venezuela<<<


use dataset_Latinobarometer_Afrobarometer_2000-2015-wide.dta, clear

preserve
keep if country==32 
replace mercosur_trust=. if mercosur_trust==0
keep if mercosur_trust!=.
rename wt weight
generate argdummymercosur = 0 if mercosur_trust <6 // Tend not to believe in effectiveness, including the undecided scoring 5
replace argdummymercosur = 1 if mercosur_trust >5 //Tend to believe 
collapse (mean) argdummymercosur [pweight=weight], by(year) //weight on (united) country level
save ARGcollapsed.dta, replace

preserve  
replace mercosur_trust=. if mercosur_trust==0
keep if mercosur_trust!=.
rename wt weight
keep if country==76 
generate bradummymercosur = 0 if mercosur_trust <6 // Tend not to believe in effectiveness, including the undecided scoring 5
replace bradummymercosur = 1 if mercosur_trust >5 //Tend to believe
collapse (mean) bradummymercosur [pweight=weight], by(year) //weight on (united) country level
save BRAcollapsed.dta, replace

preserve
replace mercosur_trust=. if mercosur_trust==0
keep if mercosur_trust!=.
rename wt weight
keep if country==862 
generate vendummymercosur = 0 if mercosur_trust <6 // Tend not to believe in effectiveness, including the undecided scoring 5
replace vendummymercosur = 1 if mercosur_trust >5 //Tend to believe
generate vendummyandean = 0 if andeanpact_trust  <6 // Tend not to believe in effectiveness, including the undecided scoring 5
replace vendummyandean = 1 if andeanpact_trust  >5 //Tend to believe
collapse (mean) vendummymercosur [pweight=weight], by(year) //weight on (united) country level
save VENcollapsed.dta, replace

preserve  
keep if country==76|country==862|country==32|country==600|country==858
replace mercosur_trust=. if mercosur_trust==0
keep if mercosur_trust!=.
rename wt weight
generate dummymercosur = 0 if mercosur_trust <6 // Tend not to believe in effectiveness, including the undecided scoring 5
replace dummymercosur = 1 if mercosur_trust >5 //Tend to believe
collapse (mean) dummymercosur [pweight=weight], by(year) //weight on (united) country level
save LB_mean_collapsed.dta, replace

use ARGcollapsed.dta, clear
merge m:m year using BRAcollapsed.dta 
drop _merge
merge m:m year using VENcollapsed.dta
drop _merge
merge m:m year using LB_mean_collapsed.dta
drop _merge
ed
save ab_mergedcollapsed.dta, replace
replace argdummymercosur = argdummymercosur * 100  
replace vendummymercosur = vendummymercosur * 100  
replace bradummymercosur = bradummymercosur * 100  
replace dummymercosur = dummymercosur * 100  
save latinob_mergedforgraphs.dta, replace


use latinob_mergedforgraphs.dta, clear
set scheme plotplain
tab year
*gen y=_n
*ed
*xlabel(1 "2001" 2 "2002" 3 "2003" 4 "2009" 5 "2013")

graph twoway ///
(connected dummymercosur   year, cmissing(no)) ///
(connected  argdummymercosur  year, cmissing(no)) ///
(connected   bradummymercosur  year, cmissing(no)) ///
(connected   vendummymercosur year, cmissing(no)), ///
 xtitle("") ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%"  80 "80%" 100 "100%") ///
legend(order(1 "Average, all Mercosur countries" 2 "Argentina" 3 "Brazil" 4 "Venezuela" )) ///
title("Perceptions of Mercosur") ytitle("% evaluate Mercosur as relatively good or very good") ///
saving(Fig2.4.gph, replace) 
 
/*Figure 2b //only for 2010 onwards
graph twoway (line eudummytrustgov gerdummytrustgov fradummytrustgov hundummytrustgov poldummytrustgov ukdummytrustgov year), ///
xtitle("") ylabel(20 "20%" 40 "40%" 60 "60%" 80 "80%") ///
legend(order(1 "EU average" 2 "Germany" 3 "France" 4 "Hungary" 5 "Poland" 6 "UK" )) ///
saving(Fig2b.gph, replace) */
 

